package com.fqyd.campustransaction.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.fqyd.campustransaction.model.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fqyd.campustransaction.model.entity.TypeGoods;
import com.fqyd.campustransaction.model.entity.TypeTask;
import com.fqyd.campustransaction.utils.Tools;

/**
 * 页面控制
 */
@Controller
@Scope(value = "prototype")
public class IndexController extends BaseController {

	@RequestMapping("index")
	public String index(HttpServletRequest request) {
		List<Goods> goodsList = goodsService.getAll();
		List<Type> types = typeService.getAll();
		List<TypeGoods> typeGoodsList = new ArrayList<>();
		for (int i = 0; i < types.size(); i++) {
			List<Goods> goods = goodsService.getByTypeId(types.get(i).getId());
			if (goods.size() > 4) {
				goods = goods.subList(0, 4);
			}
			TypeGoods typeGoods2 = new TypeGoods();
			typeGoods2.setType(types.get(i));
			typeGoods2.setGoods(goods);
			typeGoodsList.add(typeGoods2);
		}
		request.setAttribute("goodsList", goodsList);
		request.setAttribute("types", types);
		request.setAttribute("typeGoodsList", typeGoodsList);
		return "index";
	}

	@RequestMapping("goods")
	public String goods(HttpServletRequest request) {
		String typeId = request.getParameter("type_id");
		String key = request.getParameter("key");
		String sort = request.getParameter("sort");
		if (Tools.isNotEmpty(typeId)) {
			Type type = typeService.getById(typeId);
			request.setAttribute("type", type);
		}
		List<Goods> goodsList = goodsService.search(typeId, key, sort);
		List<Type> types = typeService.getAll();
		request.setAttribute("goodsList", goodsList);
		request.setAttribute("types", types);
		request.setAttribute("key", key);
		request.setAttribute("sort", sort);
		return "goods";
	}

	@RequestMapping("goods_details")
	public String goodsDetails(HttpServletRequest request) {
		String goodsId = request.getParameter("goods_id");
		if (Tools.isEmpty(goodsId)) {
			return "redirect:goods.do";
		}
		Goods goods = goodsService.getById(goodsId);
		if (goods == null) {
			return "redirect:goods.do";
		}
		Type type = typeService.getById(goods.getTypeId());
		if (type == null) {
			return "redirect:goods.do";
		}
		User saleUser = userService.getById(goods.getUserId());
		List<Type> types = typeService.getAll();
		List<Comment> comments = commentService.getByGoodId(goodsId);
		request.setAttribute("types", types);
		request.setAttribute("type", type);
		request.setAttribute("goods", goods);
		request.setAttribute("saleUser", saleUser);
		request.setAttribute("comments", comments);
		return "goods_details";
	}

	//Du:Add good comment
	@RequestMapping("goods_add_comment")
	public String goodsAddComment(HttpServletRequest request) {
		String goodsId = request.getParameter("goodIdIn");
		String userId = request.getParameter("userIdIn");
		String commentIn = request.getParameter("commentIn");
		if(!Tools.isEmpty(commentIn)){
			Comment comment = new Comment(0, goodsId, userId,commentIn);
			commentService.add(comment);
		}
		Goods goods = goodsService.getById(goodsId);
		Type type = typeService.getById(goods.getTypeId());
		User saleUser = userService.getById(goods.getUserId());
		List<Type> types = typeService.getAll();
		List<Comment> comments = commentService.getByGoodId(goodsId);
		request.setAttribute("types", types);
		request.setAttribute("type", type);
		request.setAttribute("goods", goods);
		request.setAttribute("saleUser", saleUser);
		request.setAttribute("comments", comments);
		return "goods_details";
	}

	@RequestMapping("index_task")
	public String indexTask(HttpServletRequest request) {
		List<Task> tasks = taskService.getAll();
		List<Type> types = typeService.getAll();
		List<TypeTask> typeTasks = new ArrayList<>();
		for (int i = 0; i < types.size(); i++) {
			List<Task> task = taskService.getByTypeId(types.get(i).getId());
			if (task.size() > 4) {
				task = task.subList(0, 4);
			}
			TypeTask typeTask2 = new TypeTask();
			typeTask2.setType(types.get(i));
			typeTask2.setTask(task);
			typeTasks.add(typeTask2);
		}
		request.setAttribute("tasks", tasks);
		request.setAttribute("types", types);
		request.setAttribute("typeTasks", typeTasks);
		return "index_task";
	}

	@RequestMapping("task")
	public String task(HttpServletRequest request) {
		String typeId = request.getParameter("type_id");
		String key = request.getParameter("key");
		String sort = request.getParameter("sort");
		if (Tools.isNotEmpty(typeId)) {
			Type type = typeService.getById(typeId);
			request.setAttribute("type", type);
		}
		List<Task> tasks = taskService.search(typeId, key, sort);
		List<Type> types = typeService.getAll();
		request.setAttribute("tasks", tasks);
		request.setAttribute("types", types);
		request.setAttribute("key", key);
		request.setAttribute("sort", sort);
		return "task";
	}

	@RequestMapping("task_details")
	public String taskDetails(HttpServletRequest request) {
		String taskId = request.getParameter("task_id");
		if (Tools.isEmpty(taskId)) {
			return "redirect:task.do";
		}
		Task task = taskService.getById(taskId);
		if (task == null) {
			return "redirect:task.do";
		}
		Type type = typeService.getById(task.getTypeId());
		if (type == null) {
			return "redirect:task.do";
		}
		List<Type> types = typeService.getAll();
		request.setAttribute("types", types);
		request.setAttribute("type", type);
		request.setAttribute("task", task);
		return "task_details";
	}

	@RequestMapping("news")
	public String news(HttpServletRequest request) {
		String newsId = request.getParameter("news_id");
		if (Tools.isEmpty(newsId)) {
			return "redirect:index.do";
		}
		if ("20181125224538791".equals(newsId)) {
			return "news_001";
		}
		if ("20181125224555285".equals(newsId)) {
			return "news_002";
		}
		return "redirect:index.do";
	}
}
